import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import ErrorDefault from '@/components/ErrorDefault'
export default class ErrorBoundary extends PureComponent {
    
    static propTypes = {
        renderError: PropTypes.func,
        showErrorImage: PropTypes.bool
    }

    static defaultProps = {
        showErrorImage: true
    }

    state = {
        hasError: false
    }

    static getDerivedStateFromError(error) {
        // 更新 state 使下一次渲染能够显示降级后的 UI
        return { hasError: true };
    }

    /* componentDidCatch(error, errorInfo) {
        // 你同样可以将错误日志上报给服务器
        logErrorToMyService(error, errorInfo);
    } */

    render() {
        const { showErrorImage, renderError } = this.props
        if (this.state.hasError) {
            return (
                <ErrorDefault showErrorImage={showErrorImage} renderError={renderError}/>                
            )
        }
    
        return this.props.children; 
    }
}